/**
 * 解法一：哈希表（88ms）
 */
const twoSum = function(nums, target) {
    const map = new Map();
    for (let i = 0; i < nums.length; i++) {
        const diff = target - nums[i];
        if (map.has(diff)) {
            return [map.get(diff), i];
        }
        map.set(nums[i], i);
    }
};

/**
 * 解法二：嵌套 for 循环（72ms）
 */
const twoSum2 = function(nums, target) {
    let res = [];
    const len = nums.length;
    for (let i = 0; i < len; i++) {
        for (let j = i + 1; j < len; j++) {
            if (nums[i] + nums[j] == target) {
                res.push(i);
                res.push(j);
                return res;
            }
        }
    }
};


const nums = [-2, 11, 15], target = 9;  // output: [0, 1]
// const nums = [2, 7, 11, 15], target = 9;  // output: [0, 1]
// const nums = [3, 2, 4],  target = 6;  // output: [1, 2]
// const nums = [3, 3],  target = 6;  // output: [0, 1]

const res = twoSum2(nums, target);
console.log(res);
